package com.dd.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.*;
import com.dd.util.PageList;

public class LiuDAO extends HibernateDaoSupport  {
	public void saveMain(Mainr mr) {
		try {
			getHibernateTemplate().saveOrUpdate(mr);
			
		} catch (Exception re) {
			re.printStackTrace();
		}
	}
	public void saveDetail(Detailr dr) {
		try {
			getHibernateTemplate().saveOrUpdate(dr);
		} catch (Exception re) {
			re.printStackTrace();
		}
	}
	public void delDetail(Detailr dr) {
		try {
			getHibernateTemplate().delete(dr);
		} catch (Exception re) {
			re.printStackTrace();
		}
	}
	public Detailr findDRById(java.lang.Integer id) {
		
		try {
			Detailr instance = (Detailr) getHibernateTemplate().get(
					"com.dd.entity.Detailr", id);
			return instance;
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public Mainr findMRById(java.lang.Integer id) {
		
		try {
			Mainr instance = (Mainr) getHibernateTemplate().get(
					"com.dd.entity.Mainr", id);
			return instance;
		} catch (Exception re) {
		//	re.printStackTrace();
			return null;
		}
	}
	public List findDRByMR(int root) {
		try {
			String queryString = "from Detailr where mainid=" + root+" order by numindex asc";
			return  getHibernateTemplate().find(queryString);
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public Mainr findCurMR() {
		try {
			String queryString = "from Mainr where status=1";
			List li =   getHibernateTemplate().find(queryString);
			return (Mainr)li.get(0);
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	public PageList findMRPage(int pageNo,int pageSize) {
		try {
			PageList pl = new PageList();
			String queryString = "from Mainr where status=0  ";
			String maxsizeStr = (" select count(*) " + queryString);
			Query q = this.getSession().createQuery(maxsizeStr);
			int max = 0;
			List l = q.list();
			if (l != null && !l.isEmpty()) {
				max = (Integer) l.get(0);
			}
			q = this.getSession().createQuery(queryString + " order by id desc");
			q.setFirstResult((pageNo - 1) * pageSize);
			q.setMaxResults(pageSize);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);
			return pl;
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	
	
}
